<?php
// sql/init_feedback_db.php

require_once __DIR__ . '/../vendor/autoload.php';

use Xp\Feedback\ConfigLoader;

$configLoader = new ConfigLoader(null);

try {
    $dsn = sprintf(
        '%s:host=%s;port=%s;dbname=%s;charset=%s',
        $configLoader->get('driver'),
        $configLoader->get('host'),
        $configLoader->get('port', '3306'),
        $configLoader->get('database'),
        $configLoader->get('charset')
    );

    $pdo = new PDO(
        $dsn,
        $configLoader->get('username'),
        $configLoader->get('password'),
        [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
    );

    // 读取SQL文件并执行
    $sql = file_get_contents(__DIR__ . '/init_feedback_db.sql');
    $pdo->exec($sql);

    echo "Feedback table initialized successfully.\n";
} catch (Exception $e) {
    echo "Initialization failed: " . $e->getMessage() . "\n";
}